import 'package:flutter/material.dart';
import 'package:flutter_wisave/CentrePage/Production/ProductionPage.dart';
import 'package:flutter_wisave/CentrePage/Device/EquipmentPage.dart';
import 'package:flutter_wisave/CentrePage/Quality/QualityPage.dart';
import 'package:flutter_wisave/widgets/AppColors.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:flutter/services.dart';
import 'package:flutter_wisave/CentrePage/Energy/EnergyPage.dart';
import 'package:flutter_wisave/HomePage/ModuleItem.dart';

class CentrePage extends StatelessWidget {
  CentrePage({super.key});

  final List<Map<String, dynamic>> moduleData = [
    {
      'title': '生产管理',
      'index': 0,
      'list': [
        {
          'icon': 'assets/images/centre/centre_1.png',
          'title': '实际产量',
          'index': 0,
          'content': 'Device',
          'tabindex': 0
        },
        {
          'icon': 'assets/images/centre/centre_2.png',
          'title': '产能预测',
          'index': 0,
          'content': 'Produce',
          'tabindex': 1
        },
      ]
    },
    {
      'title': '设备管理',
      'index': 1,
      'list': [
        {
          'icon': 'assets/images/centre/centre_3.png',
          'title': '故障报警',
          'index': 1,
          'content': 'Fault Alarm',
          'tabindex': 0
        },
        {
          'icon': 'assets/images/centre/centre_4.png',
          'title': '实时监控',
          'index': 1,
          'content': 'Real-time monitoring',
          'tabindex': 1
        },
        {
          'icon': 'assets/images/centre/centre_5.png',
          'title': '设备诊断',
          'index': 1,
          'content': 'Device diagnostics',
          'tabindex': 2
        },
      ]
    },
    {
      'title': '质量管理',
      'index': 2,
      'list': [
        {
          'icon': 'assets/images/centre/centre_6.png',
          'title': '钢坏温度',
          'index': 2,
          'content': 'Billet temperature',
          'tabindex': 0
        },
        {
          'icon': 'assets/images/centre/centre_7.png',
          'title': '氧化与脱碳',
          'index': 2,
          'content': 'oxidation and decarburization',
          'tabindex': 1,
        },
        {
          'icon': 'assets/images/centre/centre_8.png',
          'title': '钢坯查询',
          'index': 2,
          'content': 'Billet query',
          'tabindex': 2
        },
      ]
    },
    {
      'title': '能环管理',
      'index': 3,
      'list': [
        {
          'icon': 'assets/images/centre/centre_9.png',
          'title': '全炉热效率',
          'index': 3,
          'content': 'Heat efficiency',
          'tabindex': 0
        },
        {
          'icon': 'assets/images/centre/centre_10.png',
          'title': '能耗指标',
          'index': 3,
          'content': 'Energy consumption index',
          'tabindex': 1
        },
        {
          'icon': 'assets/images/centre/centre_11.png',
          'title': '污染物排放',
          'index': 3,
          'content': 'Pollutant emission',
          'tabindex': 2
        },
      ]
    },
    {
      'title': '智能计算',
      'index': 4,
      'list': [
        {
          'icon': 'assets/images/centre/centre_12.png',
          'title': '炉墙导热计算',
          'index': 4,
          'content': 'Furnace wall heat Conduction',
          'tabindex': 0
        },
        {
          'icon': 'assets/images/centre/centre_13.png',
          'title': '燃料燃烧计算',
          'index': 4,
          'content': 'Fuel Combustion',
          'tabindex': 1
        },
        {
          'icon': 'assets/images/centre/centre_14.png',
          'title': '风机选型计算',
          'index': 4,
          'content': 'Fan Selection',
          'tabindex': 2
        },
      ],
      'list1': [
        {
          'icon': 'assets/images/centre/centre_15.png',
          'title': '钢坯加热计算',
          'index': 4,
          'content': 'Billet Heating',
          'tabindex': 3
        },
        {
          'icon': 'assets/images/centre/centre_16.png',
          'title': '离线温度跟踪',
          'index': 4,
          'content': 'Offline Temperature Tracking',
          'tabindex': 4
        },
        {
          'icon': 'assets/images/centre/centre_17.png',
          'title': '热效率计算',
          'index': 4,
          'content': 'Calculation of Thermal Efficiency',
          'tabindex': 5
        },
      ]
    }
  ];

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, // 状态栏颜色
    ));

    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.transparent, // 使 AppBar 透明
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/home/home_topbg.png'),
              fit: BoxFit.fill,
            ),
          ),
        ),
        actions: [
          const SizedBox(width: 10),
          GestureDetector(
            onTap: () {},
            child: Image(
              image: AssetImage(AppColors.isDarkMode
                  ? 'assets/images/home/nav_logo_d.png'
                  : 'assets/images/home/nav_logo.png'),
              width: 76,
              height: 76,
            ),
          ),
          Flexible(
            child: Center(
              child: TDText(
                '智控中心',
                maxLines: 1, // 限制为单行显示
                overflow: TextOverflow.ellipsis, // 超出部分显示省略号
                style: TextStyle(
                  color: AppColors.textPrimary,
                  fontWeight: FontWeight.w500,
                  fontSize: 20,
                ),
              ),
            ),
          ),
          const SizedBox(width: 86),
        ],
      ),
      body: Stack(
        children: [
          ListView(
            children: [
              Container(
                margin: const EdgeInsets.only(left: 16.0, right: 16), // 设置左间距
                child: Text(
                  moduleData[0]['title'],
                  style: TextStyle(color: AppColors.textPrimary, fontSize: 18),
                ),
              ),
              ..._viewWithCell(moduleData[0]['list'], 167),
              Container(
                margin: const EdgeInsets.only(left: 16.0), // 设置左间距
                child: Text(
                  moduleData[1]['title'],
                  style: TextStyle(color: AppColors.textPrimary, fontSize: 18),
                ),
              ),
              ..._viewWithCell(moduleData[1]['list'], 137),
              Container(
                margin: const EdgeInsets.only(left: 16.0), // 设置左间距
                child: Text(
                  moduleData[2]['title'],
                  style: TextStyle(color: AppColors.textPrimary, fontSize: 18),
                ),
              ),
              ..._viewWithCell(moduleData[2]['list'], 137),
              Container(
                margin: const EdgeInsets.only(left: 16.0, right: 16),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      moduleData[3]['title'],
                      style:
                          TextStyle(color: AppColors.textPrimary, fontSize: 18),
                    ),
                    GestureDetector(
                      onTap: () {},
                      child: TDText(
                        '更多 >',
                        textAlign: TextAlign.right,
                        textColor: AppColors.textSecondary,
                        font: TDTheme.of(context).fontBodySmall,
                      ),
                    ),
                  ],
                ),
              ),
              ..._viewWithCell(moduleData[3]['list'], 137),
              Container(
                margin: const EdgeInsets.only(left: 16.0, right: 16), // 设置左间距
                child: Text(
                  moduleData[4]['title'],
                  style: TextStyle(color: AppColors.textPrimary, fontSize: 18),
                ),
              ),
              ..._viewWithCell(moduleData[4]['list'], 147),
              ..._viewWithCell(moduleData[4]['list1'], 147),
            ],
          ),
        ],
      ),
    );
  }
}

List<Widget> _viewWithCell(
    List<Map<String, dynamic>> moduleData, double height) {
  return [
    Container(
      width: height,
      height: 138,
      padding: const EdgeInsets.all(10),
      margin: EdgeInsets.all(height == 147 ? 0 : 10),
      // color: Colors.red,
      child: GridView.builder(
        physics: const NeverScrollableScrollPhysics(), // 禁止滑动
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: moduleData.length,
          // childAspectRatio: 1,
          mainAxisSpacing: 10, // 设置行间距
          crossAxisSpacing: 10, // 设置列间距
        ),
        itemCount: moduleData.length,
        itemBuilder: (context, index) {
          double screenWidth = MediaQuery.of(context).size.width;
          final item = moduleData[index];
          return GestureDetector(
            onTap: () {
              if (item['index'] == 0) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ProductionPage(
                      initialIndex: item['tabindex'],
                    ),
                  ),
                ).then((result) {
                  if (result != null) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(
                          'Received: $result',
                          style: TextStyle(color: AppColors.textSecondary),
                        ),
                      ),
                    );
                  }
                });
              } else if (item['index'] == 1) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          EquipmentPage(initialIndex: item['tabindex'])),
                ).then((result) {
                  if (result != null) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                          content: Text('Received: $result',
                              style:
                                  TextStyle(color: AppColors.textSecondary))),
                    );
                  }
                });
              } else if (item['index'] == 2) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => QualityPage(
                      initialIndex: item['tabindex'],
                    ),
                  ),
                ).then((result) {
                  if (result != null) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(
                          'Received: $result',
                          style: TextStyle(color: AppColors.textSecondary),
                        ),
                      ),
                    );
                  }
                });
              } else if (item['index'] == 3) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          EnergyPage(initialIndex: item['tabindex'])),
                ).then((result) {
                  if (result != null) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                          content: Text('Received: $result',
                              style:
                                  TextStyle(color: AppColors.textSecondary))),
                    );
                  }
                });
              }
            },
            child: ModuleItem(
              title: item['title'],
              icon: item['icon'],
              index: item['index'],
              content: item['content'],
              w: (screenWidth / moduleData.length) - moduleData.length * 20,
            ),
          );
        },
      ),
    ),
  ];
}
